package com.atguigu.gulimall.search.controller;

import com.atguigu.gulimall.search.service.MallSearchService;
import com.atguigu.gulimall.search.vo.SearchParams;
import com.atguigu.gulimall.search.vo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class SearchController {

    @Autowired
    private MallSearchService mallSearchService;

    @GetMapping("/list.html")
    public String listPage(SearchParams params, Model model, HttpServletRequest request) {
        String queryString = request.getQueryString();
        params.set_queryString(queryString);
        SearchResult searchResult = mallSearchService.searchParams(params);
        model.addAttribute("result", searchResult);
        return "list";
    }

    /**
     * 前台搜索处理接口
     * @param params    接收的参数集合
     * @return
     */

    @GetMapping("/search")
//    @ResponseBody
    public Object search(SearchParams params, Model model) {
        SearchResult searchResult = mallSearchService.searchParams(params);
        model.addAttribute("result", searchResult);
        return "list";
    }
}
